
insheet using "${est}/Lcorr_1sector.csv", clear
rename v1 frac_lc
rename v2 L 
keep if L<=250 

*egen L_cat = cut(L), at(0,25,50,75,100,125,150,175,200,225,250)
*replace L_cat = L_cat + 25 

*gcollapse (mean) frac_lc_model, by(L_cat)
*drop if missing(L_cat)
tempfile model 
save `model', replace 


use "${data}/APcontractlabor_final1.dta", clear
keep if treat == 1 & year == 2002


gen Lc = contract_workers
gen Lp = regular_workers
gen L = Lc + Lp
gen frac_lc = Lc/L
gen ratio = Lc/Lp
gen ind2digit = int(ind3digit/10)

*Regression coefficients that we take to the data
preserve 
qui summ L, de 
replace L = . if L>r(p99) | L<r(p1)
gen lnL = ln(L)
gen lnL2 = lnL*lnL
la var lnL "Log Firm Size"
la var lnL2 "Log Firm Size$^2$"

est clear 
eststo reg2: reghdfe frac_lc lnL lnL2 [pw=weight], absorb(ind2digit)
qui estadd scalar rsq = e(r2)
gen tempsample = e(sample)

eststo reg1: reghdfe frac_lc lnL lnL2 [pw=weight] if tempsample == 1
qui estadd scalar rsq = e(r2)

 
# delimit ;
esttab  reg1 reg2 using "${output}/calibration/TableA16.tex", replace
keep(lnL lnL2)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Fraction of Contract Workers", pattern(1 0)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("Without F.E." "Ind. F.E.")
stats(rsq N, labels("R2" "N")
fmt(2 0)) collabels(none) label booktabs nonotes
;
#delimit cr 
restore 

gen data =  1 
keep data L frac_lc
 
keep if L<=300 

append using `model', force 
replace data = 0 if missing(data)

gen ratio = frac_lc/(1-frac_lc)
gen lnL = ln(L)
gen lnR = ln(1+ratio)
keep if L>=1

local vars "frac_lc lnL"
twoway	(lpoly `vars' if data == 0 & L<300, lcolor(blue) lw(medthick) lp("..--..")) ///
	 	(lpolyci `vars' if data == 1 & L<300 , lcolor(black) lw(medthick) ///
				 ciplot(rline) blcolor(gs10) blwidth(medthick) blpattern(dash)) ///
	    , xtitle("Log Firm Size") ytitle("Fraction of Contract Workers") ///
	    legend(rows(1) pos(6) order(1 3 2) label(1 "Model") label(3 "Data") label(2 "95% C.I. Data")) ///
	    xlabel(1(1)6)
graph export "${output}/calibration/FigureA4.png", replace 

